<?php

    if (!class_exists('shopObject')) {
        require(CORE_INCLUDE_DIR.'/shopObject.php');
    }

    class mdl_logistic extends shopObject
    {
        var $idColumn = 'logi_id';
        var $textColumn = 'logi_id';
        var $adminCtl = 'plugins/logistic';
        var $defaultCols = 'logi_id,status,createtime,uptime';
        //var $defaultOrder = array('logi_id','ASC');
        var $tableName = 'sdb_express_manager_logistic';
        var $relTable = 'sdb_express_manager_logistic_item';
        var $hasTag = true;
        var $schemaIdent = 'logistic';

        function &_columns(){
            $schema = &$this->system->loadModel('utility/schemas');
            $table = substr($this->tableName,4);
            $schema_file = dirname(__FILE__).'/dbschema/'.$this->schemaIdent.'.php';
            if(is_file($schema_file)){
                $define = require($schema_file);
                $this->__table_define = &$db[$this->schemaIdent]['columns'];
            }
            return $this->__table_define;
        }

        function getColumns($filter,$from = null)
        {
            $col = parent::getColumns();
            $command_file = dirname(__FILE__).'/view/admin/logistic/finder_command.html';
            $col['_cmd'] = array('label'=>__('操作'),'width'=>75,'html'=>$command_file);
            return $col;
        }

        function searchOptions()
        {
            $column = parent::searchOptions();
            return $column;
        }

        function getFieldById($logi_id,$aField=array('*'))
        {
            $sql = 'SELECT '.implode(',',$aField).' FROM '.$this->tableName.' WHERE logi_id="'.$logi_id.'"';
            $row = $this->db->selectrow($sql);
            return $row;
        }

        function getItemsByLogiId($logi_id,$aField=array('*'))
        {
            $sql = 'SELECT '.implode(',',$aField).' FROM '.$this->relTable.' WHERE logi_id="'.$logi_id.'"';
            $data = $this->db->select($sql);
            return $data;
        }

        function gen_id(){
            $i = rand(0,9999);
            do{
                if(9999==$i){
                    $i=0;
                }
                $i++;
                $logi_id = time().str_pad($i,4,'0',STR_PAD_LEFT);
                $row = $this->db->selectrow('SELECT logi_id FROM '.$this->tableName.' WHERE logi_id =\''.$logi_id.'\'');
            }while($row);
            return $logi_id;
        }

        function checkLogiId($logi_id)
        {
            $sql = 'SELECT * FROM '.$this->tableName.' WHERE logi_id="'.$logi_id.'"';
            if ($row = $this->db->selectrow($sql)) {
                return true;
            } else {
                return false;
            }
        }

        function save($data)
        {
            /*
            if ($this->logi_id = $data['logi_id']) {
                $rs = $this->db->exec('SELECT * FROM '.$this->tableName.' WHERE logi_id='.$this->logi_id);
                $sql = $this->db->GetUpdateSql($rs,$data);
                $this->db->exec($sql);
            } else {
                if (!$data['logi_id']) {
                    $data['logi_id'] = $this->gen_id();
                }
                $this->logi_id = $data['logi_id'];
                $rs = $this->db->exec('SELECT * FROM '.$this->tableName.' WHERE 0=1');
                $sql = $this->db->GetInsertSql($rs,$data);
                $this->db->exec($sql);
            }
            */
            if (!$data['logi_id']) {
                $data['logi_id'] = $this->gen_id();
            }
            $this->logi_id = $data['logi_id'];
            $rs = $this->db->exec('SELECT * FROM '.$this->tableName.' WHERE logi_id='.$this->logi_id);
            $sql = $this->db->GetUpdateSql($rs,$data,true);
            $this->db->exec($sql);
            return $this->saveLogiItem($this->logi_id,$data['goods']);
        }

        /* 保存快运物品明细 */
        function saveLogiItem($logi_id,$aGoods)
        {
            $data = $this->db->select('SELECT * FROM '.$this->relTable.' WHERE logi_id="'.$logi_id.'"');
            if (!$data) {
                $rs = $this->db->exec('SELECT * FROM '.$this->relTable.' WHERE 0=1');
                foreach ($aGoods as $itemId =>$tmp) {
                    //$tmp['item_id'] = $itemId;
                    $tmp['logi_id'] = $logi_id;
                    $insSql = $this->db->GetInsertSql($rs,$tmp);
                    $this->db->exec($insSql);
                }
            } else {
                $aItemsKey = array();
                foreach ($data as $v) {
                    $aItemsKey[] = $v['item_id'];
                }
                $goodsKey = array_keys($aGoods);
                $insItem = array_diff($goodsKey,$aItemsKey);
                $delItem = array_diff($aItemsKey,$goodsKey);
                $upItem = array_intersect($goodsKey,$aItemsKey);
                if ($insItem) {
                    $insRes = $this->db->exec('SELECT * FROM '.$this->relTable.' WHERE 0=1');
                    foreach ($insItem as $item_id) {
                        //$aGoods[$item_id]['item_id'] = $item_id;
                        $aGoods[$item_id]['logi_id'] = $logi_id;
                        $insSql = $this->db->GetInsertSql($insRes,$aGoods[$item_id]);
                        $this->db->exec($insSql);
                    }
                }
                if ($delItem) {
                        $delSql = 'DELETE FROM '.$this->relTable.' WHERE logi_id="'.$logi_id.'" AND item_id IN ("'.implode('","',$delItem).'")';
                        $this->db->exec($delSql);
                }
                if ($upItem) {
                    foreach ($upItem as $item_id) {
                        $upRes = $this->db->exec('SELECT * FROM '.$this->relTable.' WHERE logi_id="'.$logi_id.'" AND item_id="'.$item_id.'"');
                        $upSql = $this->db->GetUpdateSql($upRes,$aGoods[$item_id]);
                        $this->db->exec($upSql);
                    }
                }
            }

            return true;
        }
    }


?>